package cn.wolfcode.car.controller;

import cn.hutool.core.date.DateUnit;
import cn.hutool.core.date.DateUtil;
import cn.hutool.core.util.RandomUtil;
import cn.wolfcode.car.domain.*;
import cn.wolfcode.car.enumpkg.ConsumptionStatusEnum;
import cn.wolfcode.car.qo.ConsumptionQueryObject;
import cn.wolfcode.car.service.*;
import com.github.pagehelper.PageInfo;
import org.apache.ibatis.annotations.Param;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import result.JsonResult;

import java.math.BigDecimal;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/consumption")
public class ConsumptionController {
    @Autowired
    private IConsumptionService consumptionService;
    @Autowired
    private IDepartmentService departmentService;
    @Autowired
    private IRoleService roleService;

    @Autowired
    private IBusinessService businessService;
    @Autowired
    private ISystemDictionaryItemService systemDictionaryItemService;
    @Autowired
    private IConsumptionItemService consumptionItemService;

    @RequestMapping("/list")
    public String list(Model model, @ModelAttribute("qo") ConsumptionQueryObject qo) {
        PageInfo<Consumption> list = consumptionService.list(qo);
        System.out.println(list);
        model.addAttribute("result", list);
        //门店
        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses", businesses);
        //状态
        ConsumptionStatusEnum[] status = ConsumptionStatusEnum.values();
        model.addAttribute("status", status);
        return "consumption/list";
    }

    @PostMapping("/saveOrUpdate")
    public String saveOrUpdate(Consumption consumption,
                               @RequestParam("categoryId") Long[] categoryIds,
                               @RequestParam("categoryItemId") Long[] categoryItemIds,
                               @RequestParam("payTypeId") Long[] payTypeIds,
                               @RequestParam("amount") BigDecimal[] amounts,
                               @RequestParam("payAmountItem") BigDecimal[] payAmounts,
                               @RequestParam("discountAmountItem") BigDecimal[] discountAmounts) {
        //获取登录人信息
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();

        if (consumption.getId() == null) {
            String s1 = DateUtil.format(new Date(), "yyyyMMddHHmmss");
            String s2 = RandomUtil.randomNumbers(5);
            consumption.setCno(s1 + s2);

            consumption.setStatus(0);
            consumption.setCreateTime(new Date());
            consumption.setCreateUserId(employee.getId());
            consumptionService.save(consumption);

            for (int i = 0; i < categoryIds.length; i++) {
                Long categoryId = categoryIds[i];
                Long categoryItemId = categoryItemIds[i];
                Long payTypeId = payTypeIds[i];
                BigDecimal amount = amounts[i];
                BigDecimal payAmount = payAmounts[i];
                BigDecimal discountAmount = discountAmounts[i];

                ConsumptionItem consumptionItem = new ConsumptionItem();
                consumption.setCno(consumption.getCno());

                consumptionItem.getCategory().setId(categoryId);
                consumptionItem.getCategoryItem().setId(categoryItemId);
                consumptionItem.getPayType().setId(payTypeId);

                consumptionItem.setAmount(amount);
                consumptionItem.setPayAmount(payAmount);
                consumptionItem.setDiscountAmount(discountAmount);
                consumptionItem.setCno(consumption.getCno());
                consumptionItem.setCreateTime(new Date());
                consumptionItem.setCreateUserId(employee.getId());

                consumptionItemService.save(consumptionItem);

            }
        } else {
            //删除原有
            consumptionItemService.deleteByCno(consumption.getCno());


            for (int i = 0; i < categoryIds.length; i++) {
                Long categoryId = categoryIds[i];
                Long categoryItemId = categoryItemIds[i];
                Long payTypeId = payTypeIds[i];
                BigDecimal amount = amounts[i];
                BigDecimal payAmount = payAmounts[i];
                BigDecimal discountAmount = discountAmounts[i];

                ConsumptionItem consumptionItem = new ConsumptionItem();
                consumption.setCno(consumption.getCno());

                consumptionItem.getCategory().setId(categoryId);
                consumptionItem.getCategoryItem().setId(categoryItemId);
                consumptionItem.getPayType().setId(payTypeId);

                consumptionItem.setAmount(amount);
                consumptionItem.setPayAmount(payAmount);
                consumptionItem.setDiscountAmount(discountAmount);
                consumptionItem.setCno(consumption.getCno());
                consumptionItem.setCreateTime(new Date());
                consumptionItem.setCreateUserId(employee.getId());

                consumptionItemService.save(consumptionItem);

            }
            consumptionService.update(consumption);
        }
        return "redirect:/consumption/list";
    }

    @PostMapping("/delete")
    @ResponseBody
    public JsonResult delete(Long id,String cno) {
        consumptionService.delete(id);
        consumptionItemService.deleteByCno(cno);
        return JsonResult.success(null);
    }

    @PostMapping("/updateStatus")
    @ResponseBody
    public JsonResult updateStatus(Long id, Integer status) {
        Subject subject = SecurityUtils.getSubject();
        Employee employee = (Employee) subject.getPrincipal();
        if (status == 1) {
            consumptionService.updateStatusShenhe(id, status, new Date(), employee.getId());
        }else{
            consumptionService.updateStatusShenheHuaiZhang(id, status, new Date(), employee.getId());
        }
        //consumptionService.updateStatus(id, status);
        return JsonResult.success(null);
    }

    @RequestMapping("/input")
    public String input(Long id, Model model) {
        if (id != null) {
            //编辑
            Consumption consumption = consumptionService.get(id);
            model.addAttribute("currentConsumption", consumption);

            List<ConsumptionItem> consumptionItems = consumptionItemService.selectBycno(consumption.getCno());
            model.addAttribute("consumptionItems", consumptionItems);

        }
        List<SystemDictionaryItem> bigType = systemDictionaryItemService.getByType(1L);
        List<SystemDictionaryItem> smallType = systemDictionaryItemService.getByType(2L);
        List<SystemDictionaryItem> payType = systemDictionaryItemService.getByType(4l);
        model.addAttribute("bigType", bigType);
        model.addAttribute("smallType", smallType);
        model.addAttribute("payType", payType);
        //门店
        List<Business> businesses = businessService.listAll();
        model.addAttribute("businesses", businesses);
        return "consumption/input";
    }


}
